#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib,"Winmm.lib")
//定义全局变量
int bird_y;
int end = 0;
int bar0_x, bar0_upy, bar0_downy;
int bar1_x, bar1_upy, bar1_downy;
int t;//撞柱后的时间
//定义小鸟
IMAGE img_bd1, img_bd2;
IMAGE bar_up1, bar_up2, bar_up3, bar_up4;//定义柱子
IMAGE bar_down1, bar_down2, bar_down3, bar_down4;
IMAGE img_bk;	//定义IMAGE对象背景

void start()
{
	mciSendString("open E:\\background.mp3 alias bkmusic", NULL, 0, NULL);//背景音
	mciSendString("play bkmusic repeat", NULL, 0, NULL);//循环播放

	initgraph(350, 600);
	bird_y = 200;
	t = 0;
	//初始化柱子0位置
	bar0_x = 300;
	bar0_upy = -300;
	bar0_downy = 400;
	//初始化柱子1位置
	bar1_x = -80;
	bar1_upy = -200;
	bar1_downy = 500;
	//读取小鸟
	loadimage(&img_bd1, "D:\\bird1.jpg");
	loadimage(&img_bd2, "D:\\bird2.jpg");
	//柱子
	loadimage(&bar_down1, "D:\\bar_down1.gif");
	loadimage(&bar_down2, "D:\\bar_down2.gif");
	loadimage(&bar_down3, "D:\\bar_down1.gif");
	loadimage(&bar_down4, "D:\\bar_down2.gif");

	loadimage(&bar_up1, "D:\\bar_up1.gif");
	loadimage(&bar_up2, "D:\\bar_up2.gif");
	loadimage(&bar_up3, "D:\\bar_up1.gif");
	loadimage(&bar_up4, "D:\\bar_up2.gif");

	loadimage(&img_bk, "D:\\background.jpg");	//读取图片到img对象中
	BeginBatchDraw();
}

void show()
{
	putimage(0, 0, &img_bk);	//在坐标（0,0）位置显示IMAGE对象	
	putimage(bar0_x, bar0_upy, &bar_up1, NOTSRCERASE);
	putimage(bar0_x, bar0_upy, &bar_up2, SRCINVERT);
	putimage(bar0_x, bar0_downy, &bar_down1, NOTSRCERASE);
	putimage(bar0_x, bar0_downy, &bar_down2, SRCINVERT);

	putimage(bar1_x, bar1_upy, &bar_up3, NOTSRCERASE);
	putimage(bar1_x, bar1_upy, &bar_up4, SRCINVERT);
	putimage(bar1_x, bar1_downy, &bar_down3, NOTSRCERASE);
	putimage(bar1_x, bar1_downy, &bar_down4, SRCINVERT);

	putimage(100, bird_y, &img_bd1, NOTSRCERASE);//小鸟,柱子显示的位置
	putimage(100, bird_y, &img_bd2, SRCINVERT);

	FlushBatchDraw();
	Sleep(50);
}
void updatewithoutInput()
{
	if (bird_y < 580)
		bird_y += 5;//小鸟下落
	else
		end = 1;//游戏结束

	if (t == 0)//未撞柱子时
	{
		if (bar0_x > -140)//柱子0左移
			bar0_x -= 3;
		else
		{
			bar0_upy = -(rand() % 300) - 200;//移到左边界，读取新的柱子位置
			bar0_downy = bar0_upy + 700;
			bar0_x = 600;
		}
		if (bar1_x > -140)//柱子1左移
			bar1_x -= 3;
		else
		{
			bar1_upy = -(rand() % 300) - 200;//移到左边界，读取新的柱子位置
			bar1_downy = bar1_upy + 700;
			bar1_x = 600;
		}
	}

	if (bar0_x <= 100 && bar0_x >= -40)//与柱子横坐标相同
	{
		if (bird_y <= bar0_upy + 600 || bird_y >= bar0_downy)//若撞柱
		{
			bird_y += 5 * t;
			t++;
		}
	}
	else if (bar1_x <= 100 && bar1_x >= -40)
	{
		if (bird_y <= bar1_upy + 600 || bird_y >= bar1_downy)//若撞柱
		{
			bird_y += 5 * t;
			t++;
		}
	}
}
void updatewithInput()
{
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input = ' ' && bird_y >= 30)//按空格键小鸟上升
		{
			bird_y -= 30;
			mciSendString("close jpmusic", NULL, 0, NULL);//先把前一次背景乐关掉
			mciSendString("open D:\\Jump.mp3 alias jpmusic", NULL, 0, NULL);//打开跳动音乐
			mciSendString("play jpmusic", NULL, 0, NULL);//循环播放
		}
	}
}
void gameover()
{
	EndBatchDraw();
	_getch();
	closegraph();
}
int main()
{
	start();//初始化
	while (1)//循环执行
	{
		show();//显示画面
		updatewithoutInput();
		updatewithInput();
		if (end == 1)//游戏结束
			break;
	}
	gameover();//游戏结束
	return 0;
}


